//
//  SISSRateStarView.m
//  test
//
//  Created by 优房网 on 2017/7/17.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSRateStarView.h"

@implementation SISSRateStarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setStarColor:(UIColor *)starColor {
    _starColor = starColor;
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    NSMutableArray *points = [NSMutableArray array];
    
    self.backgroundColor = [UIColor clearColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetFillColorWithColor(context, self.starColor.CGColor);
    CGContextSetLineWidth(context, 1);
    CGFloat radius = self.bounds.size.width * 0.5; // 半径
    CGFloat angel = (M_PI * 2) / 5; // 把园均分五等分
    for (NSInteger i = 1; i < 6; i++) {
        CGFloat x = (self.bounds.size.width * 0.5) - sinf(i * angel) * radius;
        CGFloat y = (self.bounds.size.width * 0.5) - cosf(i * angel) * radius;
        NSValue *value = [NSValue valueWithCGPoint:CGPointMake(x, y)];
        [points addObject:value];
    }
    CGContextMoveToPoint(context, [points.firstObject CGPointValue].x, [points.firstObject CGPointValue].y);
    for (NSInteger i = 1; i < 6; i++) {
        NSInteger index = (2 * i) % 5;
        CGContextAddLineToPoint(context, [points[index] CGPointValue].x, [points[index] CGPointValue].y);
    }
   
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
}

@end
